package com.spring.sunflower.common;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.spring.sunflower.App;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseCompatActivity;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.NoticeDialog;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.http.AppRetrofit;
import com.spring.sunflower.login.LoginByPhoneFruitActivity;
import com.spring.sunflower.userinfo.UserInfoEditActivity;
import com.spring.sunflower.util.AppUtils;
import com.spring.sunflower.util.CleanDataUtils;
import com.spring.sunflower.util.LocationUtils;
import com.spring.sunflower.util.SPUtils;
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMMessageReceipt;

import java.util.List;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class SettingActivity extends BaseCompatActivity implements View.OnClickListener {
    private RelativeLayout rlUserInfo, rlOnlineStatus, rlAccountType, rlAccountOff, rlBlacklist,
            rlClearChe, rlAbout, rlLogout, rlExit;
    private TextView tvCache, tvAccountType;

    @Override
    public void initView() {
        tvTitle.setText("设置中心");
        rlUserInfo = findViewById(R.id.rlUserInfo);
        rlOnlineStatus = findViewById(R.id.rlOnlineStatus);
        rlAccountType = findViewById(R.id.rlAccountType);
        rlAccountOff = findViewById(R.id.rlAccountOff);
        rlBlacklist = findViewById(R.id.rlBlacklist);
        rlClearChe = findViewById(R.id.rlClearCache);
        rlAbout = findViewById(R.id.rlAbout);
        rlLogout = findViewById(R.id.rlLogout);
        rlExit = findViewById(R.id.rlExit);
        if (!AppUtils.isVerify(SettingActivity.this)) {
            rlLogout.setVisibility(View.GONE);
        } else {
            rlLogout.setVisibility(View.VISIBLE);
        }
        tvAccountType = findViewById(R.id.tvAccountType);

        rlUserInfo.setOnClickListener(this);
        rlOnlineStatus.setOnClickListener(this);
        rlAccountType.setOnClickListener(this);
        rlAccountOff.setOnClickListener(this);
        rlBlacklist.setOnClickListener(this);
        rlClearChe.setOnClickListener(this);
        rlAbout.setOnClickListener(this);
        rlLogout.setOnClickListener(this);
        rlExit.setOnClickListener(this);
        String totalCacheSize = CleanDataUtils.getTotalCacheSize(this);
        tvCache = findViewById(R.id.tvCache);
        tvCache.setText(totalCacheSize);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onResume() {
        super.onResume();
        String mobile = getField(Constant.KEY_MOBILE, "");
        if (TextUtils.isEmpty(mobile)) {
            tvAccountType.setText("绑定手机号");
        } else {
            tvAccountType.setText("账号类型");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlUserInfo:
                startNewActivity(UserInfoEditActivity.class);
                break;
            case R.id.rlOnlineStatus:
                startNewActivity(OnlineStatusActivity.class);
                break;
            case R.id.rlAccountType:
                String mobile = getField(Constant.KEY_MOBILE, "");
                if (!TextUtils.isEmpty(mobile)) {
                    Snackbar snackbar = Snackbar.make(rlAccountType, "账号类型：" + mobile, Snackbar.LENGTH_INDEFINITE);
                    snackbar.setAction("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            snackbar.dismiss();
                        }
                    }).show();
                } else {
                    startNewActivity(BindMobileActivity.class);
                }
                break;
            case R.id.rlAccountOff:
                startNewActivity(AccountOffActivity.class);
                break;
            case R.id.rlBlacklist:
                startNewActivity(BlackListActivity.class);
                break;
            case R.id.rlClearCache:
                CleanDataUtils.clearAllCache(this);
                LoadingPopupView popupView = (LoadingPopupView) new XPopup.Builder(this)
                        .asLoading("正在清除...")
                        .show();
                popupView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String totalCacheSize = CleanDataUtils.getTotalCacheSize(SettingActivity.this);
                        tvCache.setText(totalCacheSize);
                        popupView.setTitle("清除完成");
                        popupView.dismiss();
                    }
                }, 2000);
                break;
            case R.id.rlAbout:
                startCompatActivity(AboutActivity.class);
                break;
            case R.id.rlLogout:
                NoticeDialog dialog = showNoticeDialog("提示", "退出登录提示", "确定要退出登录吗？", "确定", new OnPermissionIntroClickListener() {
                    @Override
                    public void onClickCancel() {

                    }

                    @Override
                    public void onClickOK() {
                        LocationUtils.getInstance(SettingActivity.this).stop();
                        AppRetrofit.destroy();
//                        clearStorageUserData();
                        BaseFragment.mStrangeListData.clear();
                        V2TIMManager.getMessageManager().removeAdvancedMsgListener(new V2TIMAdvancedMsgListener() {
                            @Override
                            public void onRecvNewMessage(V2TIMMessage msg) {
                                super.onRecvNewMessage(msg);
                            }

                            @Override
                            public void onRecvC2CReadReceipt(List<V2TIMMessageReceipt> receiptList) {
                                super.onRecvC2CReadReceipt(receiptList);
                            }

                            @Override
                            public void onRecvMessageRevoked(String msgID) {
                                super.onRecvMessageRevoked(msgID);
                            }

                            @Override
                            public void onRecvMessageModified(V2TIMMessage msg) {
                                super.onRecvMessageModified(msg);
                            }
                        });
                        V2TIMManager.getInstance().logout(new V2TIMCallback() {
                            @Override
                            public void onError(int i, String s) {
                                printD("ErrCode=" + i + ",msg=" + s);
                                showToast("退出失败，请稍后再试:(");
                            }

                            @Override
                            public void onSuccess() {
                                V2TIMManager.getInstance().unInitSDK();
//                                saveField(Constant.KEY_TOKEN, "");
//                                saveField(Constant.KEY_USERID, "");
                                saveField(Constant.KEY_HAS_CLICK_LOGOUT, true);
                                Intent intent = new Intent(SettingActivity.this, LoginByPhoneFruitActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);

                            }
                        });
                    }
                });
                break;
            case R.id.rlExit:
                showNoticeDialog("提示", "关闭应用提示", "确定要关闭当前应用吗？", "确定", new OnPermissionIntroClickListener() {
                    @Override
                    public void onClickCancel() {

                    }

                    @Override
                    public void onClickOK() {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
                break;

        }
    }
}
